package utils

import (
	"encoding/json"
	"fmt"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"data"`
	Msg  string      `json:"msg"`
}

const (
	CODE_SUCCESS = 1
	CODE_FAIL    = 0
)

// 获取到 Response 结构体的 JSON 字符串
func SuccessRes(data interface{}, msg string) string {
	respone := Response{
		Code: CODE_SUCCESS,
		Data: data,
		Msg:  msg,
	}

	jsonData, err := json.Marshal(respone)
	if err != nil {
		fmt.Println("JSON 编码错误:", err)
		return `{"code":0,"data":null,"msg":"JSON 编码错误"}`
	}
	// 将字节切片转换为字符串
	jsonStr := string(jsonData)
	return jsonStr
}

// 获取到 Response 结构体的 JSON 字符串
func FailRes(data interface{}, msg string) string {
	respone := Response{
		Code: CODE_FAIL,
		Data: data,
		Msg:  msg,
	}

	jsonData, err := json.Marshal(respone)
	if err != nil {
		fmt.Println("JSON 编码错误:", err)
		return `{"code":0,"data":null,"msg":"JSON 编码错误"}`
	}
	// 将字节切片转换为字符串
	jsonStr := string(jsonData)
	return jsonStr
}
